package org.nokatag.tagjava;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.system.DBCon;
import org.nokatag.system.Function;
import org.nokatag.system.RunUserFunction;

public class SelectDouble extends NokaTag{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8673727950840691086L;
	private  JspWriter jspOut =null;
	private String id = null;
	private String lietshowname=null;
	private String reightshowname=null;
	
	private String lietselectname = "Liet";
	private String rightselectname = "Right";
	private String lietselectid = "Liet";
	private String rightselectid = "Right";
	private String lietselectwidth="200px";
	private String rightselectwidth="200px";
	private String lietselectheight="200px";
	private String rightselectheight="200px";
	private String lietlist=null;
	private String rightlist=null;
	private String lietSql = null;
	private String rightSql = null;
	private String buttionClass = null;
	private String isfind=null;
	private  Connection con = null;
	private String rformatcell=null;
	private String lformatcell=null;
	private String readonly="false";//是否只读

	
	public int doStartTag()throws JspException{
		jspOut= pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		con=(con==null?DBCon.getCon():con);
		StringBuilder body = new StringBuilder();
		body.append(potjs(request));
		body.append(selectDoublejs(request));
		body.append("<div id=\""+id+"_ndv\">");
		body.append("<div id=\""+id+"\"></div>");
		body.append("<script type=\"text/javascript\">");
		//---------------显示树形菜单---------------------------
		body.append("Event.observe(document, 'dom:loaded', function() {");
		body.append("var sfg"+id+" = {");
		body.append("id:'"+id+"',");//id
		body.append("readonly:"+readonly+",");//id
		body.append("buttionClass:'"+(buttionClass==null?"but":buttionClass)+"',");//id
		body.append("repeatCheck:'"+(isfind==null?"v":isfind)+"',");
		body.append("left:{name:'"+lietselectname+"',id:'"+lietselectid+"',width:'"+lietselectwidth+"',heigth:'"+lietselectheight+"'},");
		body.append("right:{name:'"+rightselectname+"',id:'"+rightselectid+"',width:'"+rightselectwidth+"',heigth:'"+rightselectheight+"'},");
		body.append("lang:{left:'"+lietshowname+"',center:'"+Function.lung(request,"org_nokatag_tagjava_selectdouble_2")+"',right:'"+reightshowname+"',chek:'"+Function.lung(request,"org_nokatag_tagjava_selectdouble_1")+"'},");
		body.append("leftJson:"+LeftJson(request)+",");
		body.append("rightJson:"+RightJson(request));
		body.append("};");
		body.append("var nr"+id+" = new nselectdouble(sfg"+id+");");
		body.append("nr"+id+".show();");
		body.append("});");
		body.append("</script>");
		body.append("</div>");
		try{
			jspOut.write(body.toString());
		}catch(Exception se){}
		//-------------------------------------------------
		return EVAL_BODY_INCLUDE;
	}
	public String jsonForm(String va){
		return va=(va==null?"":va.replace("\'","\\'").replace("\r","\\r").replace("\n","\\n").replace("\t","\\t"));
	}
	private String LeftJson(HttpServletRequest request){
		StringBuffer LeftJsonstr = new StringBuffer();
		LeftJsonstr.append("[");
		if(lietSql!=null){
			try{
				con.setAutoCommit(true);
				Statement sta = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
				ResultSet rst = sta.executeQuery(lietSql);
				while(rst.next()){
					String value=rst.getString("value");
					String text=rst.getString("text");
					//---------------自定义---------------------------------------------
					if(lformatcell!=null && lformatcell.trim().length()>0){
						for(String fun:lformatcell.split("\\|")){
							String cn = fun.substring(fun.indexOf(" as ")+" as ".length(),fun.length());
							fun=fun.split(" as ")[0];
							String v=RunUserFunction.TagFunction(fun, request, rst);
							if(cn.equals("value")){
								value=v;
							}else if(cn.equals("text")){
								text=v;
							}
						}
					}
					//------------------------------------------------------------------
					LeftJsonstr.append("{text:'"+jsonForm(text)+"',value:'"+jsonForm(value)+"'},");
				}
				if(LeftJsonstr.length()>3)
					LeftJsonstr.delete(LeftJsonstr.length()-1, LeftJsonstr.length());
				rst.close();
				sta.close();
			}catch(Exception s){
				s.printStackTrace();
			}
		}else if(lietlist!=null){
			LeftJsonstr.append(lietlist);
		}
		LeftJsonstr.append("]");
		return LeftJsonstr.toString();
	}
	private String RightJson(HttpServletRequest request){
		StringBuffer LeftJsonstr = new StringBuffer();
		LeftJsonstr.append("[");
		if(rightSql!=null){
			try{
				Statement sta = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
				ResultSet rst = sta.executeQuery(rightSql);
				while(rst.next()){
					String value=rst.getString("value");
					String text=rst.getString("text");
					//---------------自定义---------------------------------------------
					if(lformatcell!=null && lformatcell.trim().length()>0){
						for(String fun:lformatcell.split("\\|")){
							String cn = fun.substring(fun.indexOf(" as ")+" as ".length(),fun.length());
							fun=fun.split(" as ")[0];
							String v=RunUserFunction.TagFunction(fun, request, rst);
							if(cn.equals("value")){
								value=v;
							}else if(cn.equals("text")){
								text=v;
							}
						}
					}
					//------------------------------------------------------------------
					LeftJsonstr.append("{text:'"+jsonForm(text)+"',value:'"+jsonForm(value)+"'},");
				}
				if(LeftJsonstr.length()>3)
					LeftJsonstr.delete(LeftJsonstr.length()-1, LeftJsonstr.length());
				rst.close();
				sta.close();
			}catch(Exception s){}
		}else if(rightlist!=null){
			LeftJsonstr.append(rightlist);
		}
		LeftJsonstr.append("]");
		return LeftJsonstr.toString();
	}
	public Connection getCon() {
		return con;
	}
	public void setCon(Connection con) {
		this.con = con;
	}
	
	public String getLietlist() {
		return lietlist;
	}

	public void setLietlist(String lietlist) {
		this.lietlist = lietlist;
	}

	public String getLietselectid() {
		return lietselectid;
	}

	public void setLietselectid(String lietselectid) {
		this.lietselectid = lietselectid;
	}

	public String getLietselectname() {
		return lietselectname;
	}

	public void setLietselectname(String lietselectname) {
		this.lietselectname = lietselectname;
	}

	public String getLietshowname() {
		return lietshowname;
	}

	public void setLietshowname(String lietshowname) {
		this.lietshowname = lietshowname;
	}

	public String getLietSql() {
		return lietSql;
	}

	public void setLietSql(String lietSql) {
		this.lietSql = lietSql;
	}

	public String getReightshowname() {
		return reightshowname;
	}

	public void setReightshowname(String reightshowname) {
		this.reightshowname = reightshowname;
	}

	public String getRightlist() {
		return rightlist;
	}

	public void setRightlist(String rightlist) {
		this.rightlist = rightlist;
	}

	public String getRightselectid() {
		return rightselectid;
	}

	public void setRightselectid(String rightselectid) {
		this.rightselectid = rightselectid;
	}

	public String getRightselectname() {
		return rightselectname;
	}

	public void setRightselectname(String rightselectname) {
		this.rightselectname = rightselectname;
	}

	public String getRightSql() {
		return rightSql;
	}

	public void setRightSql(String rightSql) {
		this.rightSql = rightSql;
	}
	
	public String getLietselectwidth() {
		return lietselectwidth;
	}
	public void setLietselectwidth(String lietselectwidth) {
		this.lietselectwidth = lietselectwidth;
	}
	public String getRightselectwidth() {
		return rightselectwidth;
	}
	public void setRightselectwidth(String rightselectwidth) {
		this.rightselectwidth = rightselectwidth;
	}
	public String getIsfind() {
		return isfind;
	}
	public void setIsfind(String isfind) {
		this.isfind = isfind;
	}
	public String getButtionClass() {
		return buttionClass;
	}
	public void setButtionClass(String buttionClass) {
		this.buttionClass = buttionClass;
	}
	public String getRformatcell() {
		return rformatcell;
	}
	public void setRformatcell(String rformatcell) {
		this.rformatcell = rformatcell;
	}
	public String getLformatcell() {
		return lformatcell;
	}
	public void setLformatcell(String lformatcell) {
		this.lformatcell = lformatcell;
	}
	public String getReadonly() {
		return readonly;
	}
	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public String getLietselectheight() {
		return lietselectheight;
	}
	public void setLietselectheight(String lietselectheight) {
		this.lietselectheight = lietselectheight;
	}
	public String getRightselectheight() {
		return rightselectheight;
	}
	public void setRightselectheight(String rightselectheight) {
		this.rightselectheight = rightselectheight;
	}
}

